package com.kl.drs.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.kl.drs.bean.bo.CheckResultBO;
import com.kl.drs.bean.bo.DrsAsynchTaskBO;
import com.kl.drs.common.BusinessKey;
import com.kl.drs.service.CheckResultService;
import com.kl.drs.service.DrsAsynchTaskService;
import com.kl.drs.service.RecordingAnalysisService;
import com.kl.drs.service.TaskInfoService;
import com.kl.drs.util.Log4jUtils;
/**
 * 对账异步任务
 * @author ThinkPad
 *
 */
@Component
@Configuration
@EnableScheduling
public class RecordingCheckResultJob {
	@Autowired
	private TaskInfoService taskInfoService;
	
	@Autowired
	private CheckResultService checkResultService;
	@Autowired
	private RecordingAnalysisService recordingAnalysisService;
	@Autowired
	private DrsAsynchTaskService drsAsynchTaskService;
	private static int THREAD_MAX_COUNT = 1;
	private static int currentCount = 0;
	private static int taskCount = 10;
	//每天晚上23点定时执行
	@Scheduled(cron= " 0 0 23 * * ? ")
	@Async
	public void excute(){
		Log4jUtils.logDebug("@Scheduled-------RecordingCheckResultJob.excute()---start:"+"\t\t" + new Date(System.currentTimeMillis()));
		if(currentCount<THREAD_MAX_COUNT){
			currentCount++;
			Log4jUtils.logDebug("RecordingCheckResultJob.excute()---business start:"+"\t\t" + new Date(System.currentTimeMillis()));
			boolean runFlag = true;
			while(runFlag){
				try {
					Map<String, Object> condMap = new HashMap<String, Object>();
					condMap.put("status", BusinessKey.ASYNC_TASK_STATUS_WAIT);
					condMap.put("taskType", BusinessKey.ASYNCH_TYPE_CR);
					condMap.put("taskCount", taskCount);
					List<DrsAsynchTaskBO> taskList = drsAsynchTaskService.getAsynchTaskList(condMap);
					if(taskList!=null && taskList.size()>0){
						for(DrsAsynchTaskBO drsAsynchTaskBO : taskList){
							try{
								Map<String, Object> paramMap = new HashMap<String, Object>();
								paramMap.put("taskType", BusinessKey.ASYNCH_TYPE_CR);
								paramMap.put("businessNo", drsAsynchTaskBO.getBusinessNo());
								paramMap.put("preStatus", condMap.get("status"));
								paramMap.put("status", BusinessKey.ASYNC_TASK_STATUS_DEALING);
								drsAsynchTaskService.updateTaskStatus(paramMap);
								CheckResultBO checkResultBO = new CheckResultBO();
								checkResultBO.setBusinessNo(drsAsynchTaskBO.getBusinessNo());
								CheckResultBO checkResult = checkResultService.queryCheckResult(checkResultBO);
								if(checkResult!=null && checkResult.getBusinessNo()!=null){
									try{
										boolean checkFlag = taskInfoService.checkResultTask(checkResult);
										paramMap.put("preStatus", paramMap.get("status"));
										if(checkFlag){
											paramMap.put("status", BusinessKey.ASYNC_TASK_STATUS_SUCCESS);
										}else{
											paramMap.put("status", BusinessKey.ASYNC_TASK_STATUS_FAIL_NOTHING);
										}
									}catch(Exception e1){
										Log4jUtils.logError("RecordingCheckResultJob.excute()",drsAsynchTaskBO.getBusinessNo()+"调用保信对账接口异常："+e1.getMessage(),e1);
										paramMap.put("status", BusinessKey.ASYNC_TASK_STATUS_FAIL);
										runFlag = false;
									}
								}else{
									paramMap.put("preStatus", paramMap.get("status"));
									paramMap.put("status", BusinessKey.ASYNC_TASK_STATUS_FAIL_BE);
								}
								drsAsynchTaskService.updateTaskStatus(paramMap);
							}catch(Exception ex){
								runFlag = false;
								Log4jUtils.logError("RecordingCheckResultJob.excute()",drsAsynchTaskBO.getBusinessNo()+"@Scheduled-------RecordingCheckResultJob.excute(),错误："+ex.getMessage(),ex);
							}
						}	
					}else{
						runFlag = false;
					}
				} catch (Exception e) {
					Log4jUtils.logError("RecordingCheckResultJob.excute()","@Scheduled-------RecordingCheckResultJob.excute()错误："+e.getMessage(),e);
					runFlag = false;
				}finally{
					currentCount--;
				}		
			}
			Log4jUtils.logDebug("RecordingCheckResultJob.excute()---business end:"+"\t\t" + new Date(System.currentTimeMillis()));
		}
		Log4jUtils.logDebug("@Scheduled-------RecordingCheckResultJob.excute()---end:"+"\t\t" + new Date(System.currentTimeMillis()));
	}
	}
